package com.example.behaviordemo

import android.animation.ObjectAnimator
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.Drawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.core.graphics.drawable.DrawableCompat
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.example.behaviordemo.adapter.ListPagerAdapter
import com.example.behaviordemo.fragment.*
import com.google.android.material.tabs.TabLayout

class MainActivity : AppCompatActivity() {

    private lateinit var appBarLayout: NestCollapsingToolbarLayout
    private lateinit var tabLayout: TabLayout
    private lateinit var rlTitle: RelativeLayout
    private lateinit var ivBack: ImageView
    private lateinit var ivShare: ImageView
    private lateinit var ivUserData: ImageView

    private lateinit var llSmallAuthor: LinearLayout
    private lateinit var objectAnimator: ObjectAnimator

    private lateinit var viewPager: ViewPager
    private lateinit var pagerAdapter: ListPagerAdapter

    private lateinit var tabList: MutableList<String>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initAnimator()
        initAppBar()
        initTabLayout()
        initViewPager()
    }

    private fun initAnimator() {
        llSmallAuthor = findViewById(R.id.llSmallAuthor)
        objectAnimator = ObjectAnimator.ofFloat(llSmallAuthor, "translationY", 120f, 0f)
        objectAnimator.interpolator = AccelerateInterpolator()
        objectAnimator.setDuration(300)
    }

    private fun initAppBar() {

        rlTitle = findViewById(R.id.rlTitle)
        ivBack = findViewById(R.id.ivBack)
        ivShare = findViewById(R.id.ivShare)
        ivUserData = findViewById(R.id.ivUserData)
        appBarLayout = findViewById(R.id.toolbarLayout)
        appBarLayout.setOnScrimesShowListener(object :
            NestCollapsingToolbarLayout.OnScrimsShowListener {
            override fun onScrimsShowChange(
                nestCollapsingToolbarLayout: NestCollapsingToolbarLayout,
                isScrimesShow: Boolean
            ) {
                if (isScrimesShow) {
                    rlTitle.setBackgroundColor(Color.WHITE)
                    ivBack.setImageResource(R.drawable.ic_back_blue)
                    ivShare.setImageResource(R.drawable.ic_share_blue)
                    ivUserData.setImageDrawable(
                        tintDrawable(
                            resources.getDrawable(R.drawable.ic_userdata),
                            ColorStateList.valueOf(Color.parseColor("#B6B6B6"))
                        )
                    )
                    showSmallAuthor()
                } else {
                    rlTitle.setBackgroundColor(Color.TRANSPARENT)
                    ivBack.setImageResource(R.drawable.ic_back)
                    ivShare.setImageResource(R.drawable.ic_share)
                    ivUserData.setImageDrawable(
                        tintDrawable(
                            ivUserData.drawable,
                            ColorStateList.valueOf(Color.parseColor("#FFFFFF"))
                        )
                    )
                    if (objectAnimator.isRunning) {
                        objectAnimator.cancel()
                    }
                    llSmallAuthor.visibility = View.INVISIBLE
                }
            }

        })
    }

    private fun initTabLayout() {
        tabLayout = findViewById(R.id.tabLayout)
        tabList = mutableListOf()
        tabList.add("动态")
        tabList.add("文章")
        tabList.add("专栏")
        tabList.add("沸点")
        tabList.add("其他")
    }

    private fun initViewPager() {
        viewPager = findViewById(R.id.viewPager)
        val fragments: MutableList<Fragment> = ArrayList()
        fragments.add(DynamicFragment.newInstance())
        fragments.add(ArticleFragment.newInstance())
        fragments.add(SpecialColumnFragment.newInstance())
        fragments.add(HotspotFragment.newInstance())
        fragments.add(OtherFragment.newInstance())
        pagerAdapter = object : ListPagerAdapter(supportFragmentManager, fragments) {
            override fun getPageTitle(position: Int): CharSequence? {
                if (tabList.size > position) {
                    return tabList.get(position)
                }
                return ""
            }
        }
        viewPager.adapter = pagerAdapter
        tabLayout.setupWithViewPager(viewPager)
    }

    private fun showSmallAuthor() {
        llSmallAuthor.visibility = View.VISIBLE
        objectAnimator.start()
    }

    fun tintDrawable(drawable: Drawable, colors: ColorStateList): Drawable {
        val wrappedDrawable = DrawableCompat.wrap(drawable)
        DrawableCompat.setTintList(wrappedDrawable, colors)
        return wrappedDrawable
    }
}